package com.qyhl.school.school.column.course.detail.live;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qyhl.school.R;
import com.qyhl.school.R2;
import com.qyhl.school.school.column.course.detail.SchoolColumnCourseDetailActivity;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.dialog.OtherDialog;
import com.qyhl.webtv.commonlib.entity.school.SchoolCourseDetailBean;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SchoolColumnCourseLiveFragment extends BaseFragment {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    /**
     * 数据是否第一次加载
     */
    private boolean mIsInited;
    /**
     * 布局界面初始化
     */
    private boolean mIsPrepared;

    private CommonAdapter<SchoolCourseDetailBean.CourseItem> commonAdapter;

    private List<SchoolCourseDetailBean.CourseItem> mDataList = new ArrayList<>();

    public void setmDataList(List<SchoolCourseDetailBean.CourseItem> mDataList) {
        this.mDataList = mDataList;
    }

    public static SchoolColumnCourseLiveFragment newInstance(List<SchoolCourseDetailBean.CourseItem> beanList) {
        SchoolColumnCourseLiveFragment fragment = new SchoolColumnCourseLiveFragment();
        fragment.setmDataList(beanList);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_school_column_course_live, null);
    }

    @Override
    protected void create() {
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(commonAdapter = new CommonAdapter<SchoolCourseDetailBean.CourseItem>(getContext(), R.layout.item_school_column_course_detail, mDataList) {
            @Override
            protected void convert(ViewHolder holder, SchoolCourseDetailBean.CourseItem bean, int position) {
//                holder.setText(R.id.num, "第" + (position + 1) + "讲");

                holder.setText(R.id.title, bean.getTitle());

                if (bean.isSelected()) {
                    holder.setImageResource(R.id.play_status, R.drawable.school_course_live_on_icon);
                    holder.setTextColorRes(R.id.num, R.color.global_base);
                    holder.setTextColorRes(R.id.title, R.color.global_base);

                    if (getActivity() instanceof SchoolColumnCourseDetailActivity) {
                        ((SchoolColumnCourseDetailActivity) getActivity()).videoPlay(bean.getUrl());
                    }
                } else {
                    holder.setImageResource(R.id.play_status, R.drawable.school_course_live_off_icon);
                    holder.setTextColorRes(R.id.num, R.color.global_black_lv1);
                    holder.setTextColorRes(R.id.title, R.color.global_black_lv1);
                }

                holder.setOnClickListener(R.id.play_menu, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new OtherDialog.Builder(getContext())
                                .setWidth(0.8f)
                                .setContentView(R.layout.dialog_school_course_introduction)
                                .setCancelable(true)
                                .setCanceledOnTouchOutside(true)
                                .setText(R.id.title, bean.getTitle())
                                .setScrollText(R.id.content, bean.getInfo())
                                .setDismissButton(R.id.cancel_btn)
                                .show();
                    }
                });
            }
        });
    }

    @Override
    protected void setListener() {
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                if (mDataList != null && mDataList.size() > 0 && position >= 0) {
                    SchoolCourseDetailBean.CourseItem bean = mDataList.get(position);
                    for (int i = 0; i < mDataList.size(); i++) {
                        if (position == i) {
                            mDataList.get(i).setSelected(true);
                        } else {
                            mDataList.get(i).setSelected(false);
                        }
                    }
                    commonAdapter.notifyDataSetChanged();

                    if (getActivity() instanceof SchoolColumnCourseDetailActivity) {
                        ((SchoolColumnCourseDetailActivity) getActivity()).videoPlay(bean.getUrl());
                    }
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

}
